package com.rongc.client.freight.business.carrier.view.activity;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.error.VolleyError;
import com.rongc.client.core.gson.GsonHelper;
import com.rongc.client.core.network.code.HandleCode;
import com.rongc.client.core.network.http.RequestManager;
import com.rongc.client.core.utils.ActivityUtils;
import com.rongc.client.freight.R;
import com.rongc.client.freight.base.BaseActivity;
import com.rongc.client.freight.business.carrier.view.fragment.ConveyanceDetailFragment;
import com.rongc.client.freight.business.supply.model.SupplyBean;
import com.rongc.client.freight.business.supply.request.api.SupplyWaybillDetailApi;
import com.rongc.client.freight.business.supply.view.fragment.SupplyCommentListFragment;
import com.rongc.client.freight.utils.UtilMethod;
import com.viewpagerindicator.TabPageIndicator;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created by Bruce on 2017/7/14.
 */
public class ConveyanceWaybillDetailActivity extends BaseActivity implements View.OnClickListener{

    private static final String[] CONTENT = new String[] { "车辆详情", "货主评价"};

    @Bind(R.id.pager)
    ViewPager pager;
    @Bind(R.id.indicator)
    TabPageIndicator indicator ;

    FragmentPagerAdapter adapter;
    ConveyanceDetailFragment mYJSFragment;
    SupplyCommentListFragment mWJSFragment;
    List<Fragment> mFragments = new ArrayList<Fragment>();


    String waybillId;
    SupplyBean supplyBean;

    @Bind(R.id.tv_departure)
    TextView mTvDeparture;
    @Bind(R.id.tv_destination)
    TextView mTvDestination;
    @Bind(R.id.tv_departure_tip)
    TextView mTvDepartureTip;
    @Bind(R.id.tv_destination_tip)
    TextView mTvDestinationTip;


    @OnClick({R.id.btn_submit})
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_submit:
                ActivityUtils.toast("联系货主");
                break;
        }
    }

    Response.Listener<JSONObject> respWorkersListener = new Response.Listener<JSONObject>() {

        @Override
        public void onResponse(JSONObject response) {
            if (HandleCode.requestSuccess()) {
                try {
                    supplyBean = GsonHelper.getDeserializer().fromJson(response.getString("goods"), SupplyBean.class);
                    makeTopView(supplyBean);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            } else {
                UtilMethod.dismissProgressDialog(ConveyanceWaybillDetailActivity.this);
            }
        }
    };


    Response.ErrorListener errorListener = new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            UtilMethod.dismissProgressDialog(ConveyanceWaybillDetailActivity.this);
        }
    };

    void makeTopView(SupplyBean supplyBean){
        mTvDeparture.setText(supplyBean.getStartPlace());
        mTvDestination.setText(supplyBean.getEndPlace());
    }

    @Override
    public void initParams() {
        waybillId = getIntent().getStringExtra("data");
    }

    @Override
    public void initView() {

        initToolbar(R.string.conveyance_waybill_title);

        if(mFragments != null && mFragments.size() == 0){
            mYJSFragment = ConveyanceDetailFragment.getInstance();
            mWJSFragment = SupplyCommentListFragment.getInstance();
            mFragments.add(mYJSFragment);
            mFragments.add(mWJSFragment);
        }

        adapter = new GoogleMusicAdapter(getSupportFragmentManager());

        pager.setAdapter(adapter);
        pager.setOffscreenPageLimit(2);

        indicator.setVisibility(View.VISIBLE);
        indicator.setViewPager(pager);

        SupplyWaybillDetailApi p = new SupplyWaybillDetailApi(new SupplyWaybillDetailApi.SupplyWaybillDetailParams(waybillId),
                respWorkersListener, errorListener);
        RequestManager.getInstance().call(p);


    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_conveyance_supply_detail;
    }

    class GoogleMusicAdapter extends FragmentPagerAdapter {
        public GoogleMusicAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            switch (position) {
                case 0:
                    if(mYJSFragment == null)
                        mYJSFragment = ConveyanceDetailFragment.getInstance();
                    return mYJSFragment;
                case 1:
                if(mWJSFragment == null) {
                        mWJSFragment = SupplyCommentListFragment.getInstance();
                    }
                return mWJSFragment;
                default:
                    break;
            }

            return mFragments.get(position);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return CONTENT[position % CONTENT.length].toUpperCase();
        }

        @Override
        public int getCount() {
            return CONTENT.length;
        }
    }
}
